var t = require("../../utils/util.js"), a = getApp(), e = null, n = null;

Page({
    data: {
        storeId: a.globalData.storeId || "",
        store: a.globalData.store || "",
        isIpx: a.globalData.isIpx,
        inventories: [],
        confirmModal: !1
    },
    onLoad: function(t) {
        var e = this;
        console.info("options", t), wx.showLoading({
            title: "获取可用档期"
        });
        var n = new Date(), i = n.getFullYear(), o = n.getMonth() + 1, r = n.getFullYear(), s = n.getMonth() + 1, c = n.getDate(), u = [ "日", "一", "二", "三", "四", "五", "六" ];
        this.setData({
            cur_year: r,
            cur_month: s,
            cur_day: c,
            today: this.formatDate(r, s, c),
            now_year: i,
            now_month: o,
            weeks_ch: u,
            cur_weekday: null,
            cur_date: null,
            cur_inventories: [],
            cur_inventory: null,
            cur_inventory_label: ""
        }), a.globalData.storeId = t.store_id, this.initStoreSchedule(t.store_id, function() {
            e.calculateEmptyGrids(r, s), e.calculateDays(r, s);
        }), a.globalData.campaign && a.campaignEventLog("view_calendar");
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    initStoreSchedule: function(e, n) {
        var i = this, o = this.data.today.replace(/\-/g, "");
        a.getRequest(a.globalData.BASE_URL + "/stores/" + e + "/calendar?from=" + o + "&to=20191231", {}).then(function(e) {
            var o = [];
            e.forEach(function(e) {
                "weekend" === a.globalData.cart.inventory_restrictions && t.isWeekend(e.date) || o.push(e);
            });
            var r = (o = o.sort(function(t, a) {
                return +new Date(t.date) - +new Date(a.date);
            }))[o.length - 1].date, s = r.split("-")[0] - 0, c = r.split("-")[1] - 0, u = r.split("-")[2] - 0;
            o.forEach(function(t) {
                t.inventories.forEach(function(t) {
                    t.sku_title = t.sku.indexOf("am") > -1 ? "9:30" : "14:30";
                });
            }), i.setData({
                inventories: o,
                the_last_date: r,
                the_last_year: s,
                the_last_month: c,
                the_last_day: u
            }), n && n();
        });
    },
    convertCalendarAPI: function(t) {
        var a = {}, e = [];
        t.forEach(function(t) {
            var e = void 0;
            a[t.date] || (a[t.date] = []), a[t.date].forEach(function(a) {
                a.sku === t.sku && (e = !0);
            }), !e && a[t.date].push({
                sku: t.sku,
                sku_title: t.sku.indexOf("am") > -1 ? "9:30" : "14:30",
                version: t.version,
                id: t.id
            });
        });
        for (var n in a) e.push({
            date: n,
            inventories: a[n].sort(function(t, a) {
                return a.sku < t.sku;
            })
        });
        return e;
    },
    getInventoriesByDate: function(t) {
        var a = [];
        return this.data.inventories && this.data.inventories.forEach(function(e) {
            e.date === t && (a = e.inventories);
        }), a;
    },
    getWeekdayStrByDate: function(t) {
        return this.data.weeks_ch[new Date(t).getDay()];
    },
    chooseInventory: function(t) {
        var e = t.detail.value, n = void 0, i = this.data.cur_date && this.getWeekdayStrByDate(this.data.cur_date);
        this.data.inventories.forEach(function(t) {
            t.inventories.forEach(function(t) {
                t.id === e && (n = t);
            });
        }), console.info("inventoryId", e), console.info("inventory", n), console.info("weekday", i), 
        this.setData({
            cur_inventory: n,
            cur_weekday: i,
            cur_inventory_label: this.data.cur_year + "年" + this.data.cur_month + "月" + this.data.cur_day + "日(周" + i + ") " + n.sku_title
        }), a.globalData.cart.inventory = n;
    },
    checkDateValid: function(t) {
        console.log("choosedDate", t);
        var a = !1;
        return this.data.inventories.forEach(function(e) {
            e.date == t && (a = !0);
        }), a;
    },
    getThisMonthDays: function(t, a) {
        return new Date(t, a, 0).getDate();
    },
    getFirstDayOfWeek: function(t, a) {
        return new Date(Date.UTC(t, a - 1, 1)).getDay();
    },
    calculateEmptyGrids: function(t, a) {
        var e = this.getFirstDayOfWeek(t, a), n = [];
        if (e > 0) {
            for (var i = 0; i < e; i++) n.push(i);
            this.setData({
                hasEmptyGrid: !0,
                empytGrids: n
            });
        } else this.setData({
            hasEmptyGrid: !1,
            empytGrids: []
        });
    },
    calculateDays: function(t, a) {
        for (var e = [], n = this.getThisMonthDays(t, a), i = new Date().getDate(), o = 1; o <= n; o++) {
            var r = this.formatDate(t, a, o);
            e.push({
                day: o,
                choosed: o == i,
                date: r,
                inventories: this.getInventoriesByDate(r)
            });
        }
        this.setData({
            days: e
        }), wx.hideLoading();
    },
    handleCalendar: function(t) {
        var a = t.currentTarget.dataset.handle, e = this.data.cur_year, n = this.data.cur_month;
        if ("prev" === a) {
            var i = n - 1, o = e;
            i < 1 && (o = e - 1, i = 12), this.calculateDays(o, i), this.calculateEmptyGrids(o, i), 
            this.setData({
                cur_year: o,
                cur_month: i
            });
        } else {
            var r = n + 1, s = e;
            r > 12 && (s = e + 1, r = 1), this.calculateDays(s, r), this.calculateEmptyGrids(s, r), 
            this.setData({
                cur_year: s,
                cur_month: r
            }), console.log("handleCalendar", this.data);
        }
    },
    formatDate: function(t, a, e) {
        return t + "-" + (a > 9 ? a : "0" + (a - 0)) + "-" + (e > 9 ? e : "0" + (e - 0));
    },
    chooseDate: function(t) {
        console.log("this.data.selected", this.data.selected);
        var a = t.currentTarget.dataset.idx, e = this.data.days, n = this.formatDate(this.data.cur_year, this.data.cur_month, e[a].day);
        this.checkDateValid(n) && (e.forEach(function(t) {
            t.choosed = !1;
        }), this.data.selected = e[a].date, this.setData({
            days: e,
            cur_date: this.data.selected,
            cur_day: e[a].day,
            cur_inventories: e[a].inventories,
            cur_inventory: null,
            cur_inventory_label: null
        }));
    },
    chooseYearAndMonth: function() {
        var t = this.data.cur_year, a = this.data.cur_month;
        e = this.data.cur_year, n = this.data.cur_month;
        for (var i = [], o = [], r = 1900; r <= 2100; r++) i.push(r);
        for (var s = 1; s <= 12; s++) o.push(s);
        var c = i.indexOf(t), u = o.indexOf(a);
        this.setData({
            picker_value: [ c, u ],
            picker_year: i,
            picker_month: o,
            showPicker: !0
        });
    },
    pickerChange: function(t) {
        var a = t.detail.value;
        e = this.data.picker_year[a[0]], n = this.data.picker_month[a[1]];
    },
    tapPickerBtn: function(t) {
        var a = {
            showPicker: !1
        };
        "confirm" === t.currentTarget.dataset.type && (a.cur_year = e, a.cur_month = n, 
        this.calculateEmptyGrids(e, n), this.calculateDays(e, n)), this.setData(a);
    },
    closeModal: function() {},
    openModal: function() {
        var t = this.data.cur_inventory_label;
        this.data.store.name;
        wx.showModal({
            title: "确认已选档期",
            content: "" + t,
            showCancel: !0,
            cancelText: "重选",
            cancelColor: "#C2A469",
            confirmText: "确认",
            confirmColor: "#259B24",
            success: function(e) {
                e.confirm && (a.globalData.campaign && a.campaignEventLog("confirm_date"), a.globalData.cart.inventory_label = t, 
                wx.navigateTo({
                    url: "/pages/order-info/order-info",
                    success: function(t) {},
                    fail: function(t) {},
                    complete: function(t) {}
                }));
            },
            fail: function(t) {},
            complete: function(t) {}
        });
    },
    confirmHandler: function() {
        wx.navigateTo({
            url: "/pages/order-detail/order-detail",
            success: function(t) {},
            fail: function(t) {},
            complete: function(t) {}
        }), this.setData({
            confirmModal: !1
        });
    }
});